package com.hut.mst.service.impl;

import com.hut.mst.service.UpLoadService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;

/**
 * @author JunHao Deng
 * @date 2021/04/01 11:59
 * @description：
 **/
@Service
public class UpLoadServicelmpl  implements UpLoadService {
    @Override
    public Boolean registerByUpLoad(MultipartFile file, HttpSession session) throws IOException {
        //指定文件夹
        String path = session.getServletContext().getRealPath("/upload");
        File uploadFile = new File(path);
        //不存在，即创建
        if (!uploadFile.exists()){
            uploadFile.mkdir();
        }
        //将上传文件重命名,并处理,用当前时间戳重命名
        String filename = file.getOriginalFilename();
        String ext = filename.substring(filename.lastIndexOf("."));
        long timeMillis = System.currentTimeMillis();
        String newname = timeMillis+ext;
        //上传
        file.transferTo(new File(path+"/"+newname));

        //上传照片
        //        //上传到对应uid
//        String user_photo = "upload/"+newFileName;
//        Integer uid = Integer.parseInt(session.getAttribute("uid").toString());
//        userService.uploaduser_photo(uid,user_photo);
//        session.setAttribute("user_photo",user_photo);
        return null;
    }


}
